package com.yidian.demotickets.SAXPrint;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.yidian.demotickets.entity.SvgContentEntity;
import com.yidian.demotickets.entity.SvgEntity;
import com.yidian.demotickets.utils.AidlUtil;
import com.yidian.demotickets.utils.BytesUtil;
import com.yidian.demotickets.utils.ESCUtil;
import org.xml.sax.SAXException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by bmc on 2017/10/23.
 */

public class SAXPrint {
    private Context mContext;
    private InputStream inputStream;

    public SAXPrint(Context mContext, InputStream inputStream) {
        this.mContext = mContext;
        this.inputStream = inputStream;
    }

    public SvgEntity jiexi() {
      SvgEntity  svgEntity=new SvgEntity();
        try {
            SAXParser parser= SAXParserFactory.newInstance().newSAXParser();
            MySaxHandler mySaxHandler=new MySaxHandler();

            try {
                parser.parse(inputStream, mySaxHandler);
                inputStream.close();//关闭文件流

                svgEntity=mySaxHandler.getSvgEntity();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return  svgEntity;
    }

    public void print() {
        SvgEntity svgEntity=jiexi();//解析实体

        Log.d("qwer",svgEntity.toString());

        List<SvgContentEntity> list=svgEntity.getContents();
        SvgContentEntity contentEntity=null;
        for (int i=0;i<list.size();i++){
            contentEntity=list.get(i);
            int category=contentEntity.getCategory();//类型
            switch (category){
                case 0://代表纯文本
                    boolean bold=false;
                    bold=(contentEntity.getBold()==1)?true:false;
                    AidlUtil.getInstance().printText(contentEntity.getContent(),contentEntity.getAlign(),contentEntity.getFontSize()*2,bold,false);
                    if(contentEntity.getLinewrap()==1){//如果实体中有换行标志则换一行
                        AidlUtil.getInstance().wrapNLine(1);
                    }

                    break;
                case 1://代表line  画一条间断线
                    AidlUtil.getInstance().sendRawData(ESCUtil.printBitmap(BytesUtil.initJianDuanXian(2,(384+300))));

                    break;
                case 2://代表商品单
                    int row=contentEntity.getRow();
                   // row+=1;
                    int colum=contentEntity.getColum();

                    String[]contentString=new String[colum];
                    int []weight=new int[colum];
                    int []align=new int[colum];

                    SvgContentEntity temp_entity=new SvgContentEntity();
                    for(int e=1;e<=row;e++){//打印row行column列 带上表头 （商品名  数量  价格）
                        int r=0;int dis[]=new int[colum];
                        for(int f=1;f<=colum;f++){
                            temp_entity=list.get((e-1)*colum+f+i-1);
                            contentString[r]=list.get((e-1)*colum+f+i-1).getContent();
                            align[r]=list.get((e-1)*colum+f+i-1).getAlign();

                            dis[r]=list.get((e-1)*colum+f+i-1).getDisx();
                            Log.d("qwer",""+dis[r]);
                            r++;
                        }
                        //计算该行每一列的weight值
                        weight[colum-1]=100-dis[colum-1];
                        for(int v=colum-2;v>=0;v--){
                            weight[v]=dis[v+1]-dis[v];
                            Log.d("qwer",""+weight[v]);
                        }

                        //判断比重是否是正确的
                        int sum=0,flag=0;
                        for(int u=0;u<colum;u++){
                            if(!(weight[u]>0&&weight[u]<100)) {
                                flag=1; //比重有误
                            }
                            sum+=weight[u];
                        }
                        if(sum!=100||flag==1) {//如果比重之和不为100或者是每个比重不在0--100之间的
                            Toast.makeText(mContext, "打印数据有误", Toast.LENGTH_SHORT).show();
                        }else{
                            AidlUtil.getInstance().printOneTable(contentString,weight,align);
                        }

                    }

                    i=i+(row*colum-1);//将i修改为i+row*colum
                    contentString=null;
                    weight=null;
                    align=null;
                    break;
                case 3://代表二维码
                    String qrcode=contentEntity.getContent();
                    AidlUtil.getInstance().printQr(qrcode,8,3);
                    if(contentEntity.getLinewrap()==1){//如果实体中有换行标志则换一行
                        AidlUtil.getInstance().wrapNLine(1);
                    }

                    break;
            }

            AidlUtil.getInstance().wrapNLine(1);
        }

        AidlUtil.getInstance().cutPaper();
    }


}
